﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: CPU
* Version: 0.2
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["cpu"] = new Object();
myPlugins["cpu"]["name"] = getLocalizedString2("mod_sysm_plugin_cpu_name");
myPlugins["cpu"]["onlyonce"] = true;
myPlugins["cpu"]["newObject"] = function (NewSettings) { return new plugin_cpu_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_cpu_sensor(NewSettings) {
	// Required variables
	this.plugin = "cpu";
	this.settings = NewSettings;
	this.edit = plugin_cpu_edit;
	this.ReDraw = plugin_cpu_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor) this.settings.barcolor = "#0000FF";
	if(this.settings.MultiCPU!=true) this.settings.MultiCPU = false;
	if(this.settings.DynColor!=true) this.settings.DynColor = false;
	if(this.settings.ClockSpeed!=true) this.settings.ClockSpeed = false;
	if(this.settings.Fader!=true) this.settings.Fader = false;
	if(!this.settings.bc1) this.settings.bc1 = "#008000";
	if(!this.settings.bc2) this.settings.bc2 = "#AFAF00";
	if(!this.settings.bc3) this.settings.bc3 = "#FF7F00";
	if(!this.settings.bc4) this.settings.bc4 = "#FF0000";

	this.FreqUpdate = 1;


	// Find all CPU's for windows
	this.cpu = new Array();
	if(system.platform=="windows") {
		for(var i=0;i<32;i++) {
			try {
				var tmp = wmi.Get( 'Win32_PerfRawData_PerfOS_Processor.Name="' + i + '"' ).Properties_.Item( "PercentProcessorTime" ).Value;
				if(tmp>0) this.cpu[this.cpu.length] = i;
			} catch (e) {  }
		}
		this.cpucount = this.cpu.length;
	} else {
		// For non-Windows only single CPU display is supported
		this.cpucount = 1;
	}
	if(mod_sysmonitor_debug>1) print("Found "+this.cpucount+" CPU(s)");

	if(this.cpucount<1) this.cpucount = 1;  // Workaround if WMI returns garbage!


	// Create PercentBar's
	this.cpubar = new Array();
	for(var i = 0 ; i < this.cpucount ; i++){
		if(this.cpucount == 1) {
			if(this.settings.ClockSpeed==true)
				this.cpubar[i] = new class_PercentBar(this.frame,0,(i * mod_sysmonitor_size),"CPU @ " + plugin_cpu_WMI_CurrentClockSpeed(0));
			else
				this.cpubar[i] = new class_PercentBar(this.frame,0,(i * mod_sysmonitor_size),"CPU");
		} else {
			if(this.settings.ClockSpeed==true)
				this.cpubar[i] = new class_PercentBar(this.frame,0,(i * mod_sysmonitor_size),"CPU " + (i + 1) + " @ " + plugin_cpu_WMI_CurrentClockSpeed(this.cpu[i]));
			else
				this.cpubar[i] = new class_PercentBar(this.frame,0,(i * mod_sysmonitor_size),"CPU " + (i + 1));
			this.cpubar[i].oldvalue = plugin_cpu_WMI_PercentProcessorTime(this.cpu[i]);
			if(i>0) this.cpubar[i].PercentBarVisible(this.settings.MultiCPU);
		}
		this.cpubar[i].PercentColor(this.settings.color);
		if(this.settings.DynColor!=true) this.cpubar[i].PercentBarColor(this.settings.barcolor);
	}
	if(this.settings.MultiCPU==false) {
		this.cpucount = 1;
		this.cpubar[0].text_name.data = "CPU";
	}


	// Update Timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = 1;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_cpu_update.call(obj); };
	plugin_cpu_update.call(obj);


	// Calculating sensore height (with background)
	this.frame.height = this.cpubar[this.cpucount-1].background.vOffset + this.cpubar[this.cpucount-1].background.height;
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_cpu_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_barcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_multi");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.MultiCPU;
		if(system.platform!="windows")
			Drive_Edit_DialogFields[FieldID].hidden = 1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_fading");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.Fader;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_clockspeed");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.ClockSpeed;
		Drive_Edit_DialogFields[FieldID].description = getLocalizedString2("mod_sysm_plugin_hdd_pref_clockspeeddesc");
		if(system.platform!="windows")
			Drive_Edit_DialogFields[FieldID].hidden = 1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_dyncolor");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.DynColor;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_bc1");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.bc1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_bc2");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.bc2;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_bc3");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.bc3;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_hdd_pref_bc4");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.bc4;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			if(this.settings.MultiCPU != Drive_Edit_DialogResults[2]) {
				this.settings.MultiCPU = Drive_Edit_DialogResults[2];
				for(var i = 1 ; i < this.cpu.length ; i++){
					this.cpubar[i].PercentBarVisible(this.settings.MultiCPU);
				}

				if(this.settings.MultiCPU==true) this.cpucount = this.cpu.length;
				else this.cpucount = 1;

				if(this.cpucount<1) this.cpucount = 1; // Workaround if WMI returns garbage!

				// Re-Calculating sensore height (with background)
				this.frame.height = this.cpubar[this.cpucount-1].background.vOffset + this.cpubar[this.cpucount-1].background.height;
			}

			if(this.settings.Fader != Drive_Edit_DialogResults[3]) {
				this.settings.Fader = Drive_Edit_DialogResults[3];
			}

			if(this.settings.ClockSpeed != Drive_Edit_DialogResults[4]) {
				this.settings.ClockSpeed = Drive_Edit_DialogResults[4];
				if(this.settings.ClockSpeed == true)
					this.FreqUpdate = 1;
			}

			this.settings.color = Drive_Edit_DialogResults[0];
			this.settings.barcolor = Drive_Edit_DialogResults[1];
			this.settings.DynColor = Drive_Edit_DialogResults[5];
			this.settings.bc1 = Drive_Edit_DialogResults[6];
			this.settings.bc2 = Drive_Edit_DialogResults[7];
			this.settings.bc3 = Drive_Edit_DialogResults[8];
			this.settings.bc4 = Drive_Edit_DialogResults[9];

			for(var i = 0 ; i < this.cpu.length ; i++){
				this.cpubar[i].PercentColor(this.settings.color);
				if(this.settings.DynColor==false) this.cpubar[i].PercentBarColor(this.settings.barcolor);
				if(this.settings.Fader==false) {this.cpubar[i].PercentBarPeakFader(999); this.cpubar[i].text_used.data = "";}
				if(this.settings.ClockSpeed==false) this.cpubar[i].text_name.data = "CPU " + (i+1);
			}
			if(this.settings.ClockSpeed==false&&this.cpucount==1) this.cpubar[0].text_name.data = "CPU";

			mod_sysmonitor_ReDraw();
			//plugin_cpu_update.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}


	var plugin_cpu_update_tmp_newvalue;
	function plugin_cpu_update() {
		for(var i = 0 ; i < this.cpucount ; i++){
			if(this.cpucount == 1) {
				this.cpubar[i].PercentBarValue(system.cpu.activity,"");
			} else {
				plugin_cpu_update_tmp_newvalue = plugin_cpu_WMI_PercentProcessorTime(this.cpu[i]);
				this.cpubar[i].PercentBarValue( (100-(plugin_cpu_update_tmp_newvalue-this.cpubar[i].oldvalue)/100000) ,"");
				this.cpubar[i].oldvalue = plugin_cpu_update_tmp_newvalue;
			}
	
			if(this.settings.DynColor == true) {
				if(this.cpubar[i].middle.width > 95)
					this.cpubar[i].PercentBarColor(this.settings.bc4);
				else if(this.cpubar[i].middle.width > 75)
					this.cpubar[i].PercentBarColor(this.settings.bc3);
				else if(this.cpubar[i].middle.width > 50)
					this.cpubar[i].PercentBarColor(this.settings.bc2);
				else
					this.cpubar[i].PercentBarColor(this.settings.bc1);
			}
	
			if(this.settings.Fader == true) {
				this.cpubar[i].text_used.data = this.cpubar[i].PercentBarPeakFader(3,4) + "%";
			}
	
			if(this.settings.ClockSpeed == true) {
				if((system.platform=="windows") && (--this.FreqUpdate<=0)) {
					this.FreqUpdate=60;
					if(this.cpucount == 1) {
						this.cpubar[i].text_name.data = "CPU @ " + plugin_cpu_WMI_CurrentClockSpeed(this.cpu[i]);
					} else {
						this.cpubar[i].text_name.data = "CPU " + (i+1) + " @ " + plugin_cpu_WMI_CurrentClockSpeed(this.cpu[i]);
					}
				}
			}
	
		}
		return true;
	}

	function plugin_cpu_WMI_PercentProcessorTime(CPUID) {
		try {
			//return wmi.Get( 'Win32_PerfFormattedData_PerfOS_Processor.Name="' + CPUID + '"' ).Properties_.Item( "PercentProcessorTime" ).Value;
			return wmi.Get( 'Win32_PerfRawData_PerfOS_Processor.Name="' + CPUID + '"' ).Properties_.Item( "PercentIdleTime" ).Value;
		} catch (e) {
			print("WMI Error (plugin_cpu_WMI_PercentProcessorTime): " + e);
			return 0;
		}
	}

	function plugin_cpu_WMI_CurrentClockSpeed(CPUID) {
		try {
			return wmi.Get( 'Win32_Processor.DeviceID="CPU' + CPUID + '"' ).Properties_.Item( "CurrentClockSpeed" ).Value;
		} catch (e) {
			print("WMI Error (plugin_cpu_WMI_CurrentClockSpeed): " + e);
			return 0;
		}
	}

	function plugin_cpu_ReDraw() {
		this.frame.width = mod_sysmonitor_width;

		for(var i = 0; i < this.cpubar.length ; i++) {
			this.cpubar[i].style = preferences.mod_sysmonitor_bar_style.value;
			this.cpubar[i].size = mod_sysmonitor_size;
			this.cpubar[i].vOffset = i * mod_sysmonitor_size;
		}

		// Re-Calculating sensore height (with background)
		this.frame.height = this.cpubar[this.cpucount-1].background.vOffset + this.cpubar[this.cpucount-1].background.height;

		return true;
	}

}

// --------------------------------------------------------------------------------
// The END